package com.czd.support.util;


import com.czd.support.enums.CodeTypeEnum;

import java.util.Random;

/**
 * Created with IntelliJ IDEA.
 * Author: Jerry.hu
 * Create: Jerry.hu (2014/8/25 23:10)
 * Description: //TODO 文件描述
 * To change this template use File | Settings | File Templates.
 */
public class VerifyCodeUtils {
	static Random r = new Random();
	static String ssource =  "0123456789";
	static String letter = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	static char[] src = ssource.toCharArray();
	static char[] letters = letter.toCharArray();

	public static String runVerifyCode(int length,CodeTypeEnum codeTypeEnum){
		if(codeTypeEnum.equals(CodeTypeEnum.数字)){
			return randCode(length,src);
		}else if(codeTypeEnum.equals(CodeTypeEnum.数字字母)){
			return randCode(length,letters);
		}
		return null;
	}

	//产生随机字符串
	private static String randCode(int length,char [] src){
		char[] buf = new char[length];
		int rnd;
		for(int i=0;i<length;i++){
			rnd = Math.abs(r.nextInt()) % src.length;
			buf[i] = src[rnd];
		}
		return new String(buf);
	}
	public static void main(String[] args) {
		System.out.println(runVerifyCode(6, CodeTypeEnum.数字字母));
	}
}

